package com.shop2cn.iapi.sqidcard.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.sqidcard.parameter.SearchIdcardInfoByIdReq;
import com.shop2cn.iapi.sqidcard.parameter.resp.IdcardInfoVo;
import com.shop2cn.iapi.sqidcard.service.SearchIdcardInfoByIdCall;
import com.ymatou.common.lang.CipherUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.sqidcard.IdcardInfo;
import com.ymttest.database.sqlwapper.sqidcard.IdcardInfoWrapper;

public class Ts_SearchIdcardInfoById {
	
	//java接口
	private static SearchIdcardInfoByIdReq searchIdcardInfoByIdReq;
	private static SearchIdcardInfoByIdCall searchIdcardInfoByIdCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据userId和idcardId获取身份证信息");
	}

	@Before
	public void caseUp() {
		searchIdcardInfoByIdReq = new SearchIdcardInfoByIdReq();
		searchIdcardInfoByIdCall = new SearchIdcardInfoByIdCall();	
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(searchIdcardInfoByIdCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public void verifyDb() throws Exception {
		
		String newKey = "123456789";
		IdcardInfoWrapper idcardInfoWrapper = new IdcardInfoWrapper();

		IdcardInfo expectResult = idcardInfoWrapper.selectByIdcardIdAndUserId(
				searchIdcardInfoByIdReq.getIdcardId(),
				searchIdcardInfoByIdReq.getUserId());
		IdcardInfoVo actualResult = searchIdcardInfoByIdCall
				.getIdcardInfoVo();

		Logger.verifyEquals(expectResult.getIdcard_name(),
				actualResult.getIdcardName(), "验证Idcard_name");
		Logger.verifyEquals(
				CipherUtil.decryptAES(newKey, expectResult.getIdcard_no()),
				actualResult.getIdcardNo(), "验证Idcard_no");
		Logger.verifyEquals(expectResult.getUser_id(),
				actualResult.getUserId(), "验证User_id");
		Logger.verifyEquals(expectResult.getIdcard_id(),
				actualResult.getIdcardId(), "验证Idcard_id");
		Logger.verifyEquals(expectResult.getPush_status(),
				actualResult.getPushStatus(), "验证Push_status");
		
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SearchIdcardInfoById_001() {
		Logger.start(true, "获取推送状态为false的身份证信息");
		
		try {
			
			searchIdcardInfoByIdReq.setIdcardId(7L);
			searchIdcardInfoByIdReq.setUserId(645967000000L);
			
			searchIdcardInfoByIdCall.setData(searchIdcardInfoByIdReq);
			searchIdcardInfoByIdCall.callService();
			
			Logger.verifyEquals(true, searchIdcardInfoByIdCall.getSuccess(), "验证success");
			
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SearchIdcardInfoById_002() {
		Logger.start(true, "获取推送状态为true的身份证信息");
		
		try {
			
			searchIdcardInfoByIdReq.setIdcardId(8L);
			searchIdcardInfoByIdReq.setUserId(645967000000L);
			
			searchIdcardInfoByIdCall.setData(searchIdcardInfoByIdReq);
			searchIdcardInfoByIdCall.callService();
			
			Logger.verifyEquals(true, searchIdcardInfoByIdCall.getSuccess(), "验证success");
			
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SearchIdcardInfoById_003() {
		Logger.start(true, "不输入idcardID");
		
		try {
			
			searchIdcardInfoByIdReq.setIdcardId(null);
			searchIdcardInfoByIdReq.setUserId(645967000000L);
			
			searchIdcardInfoByIdCall.setData(searchIdcardInfoByIdReq);
			searchIdcardInfoByIdCall.callService();
			
			Logger.verifyEquals(false, searchIdcardInfoByIdCall.getSuccess(), "验证success");
			Logger.verifyEquals(100, searchIdcardInfoByIdCall.getErrorCode(), "验证errorCode");
			Logger.verifyEquals("idcardId:must be greater than or equal to 1",
					searchIdcardInfoByIdCall.getMsg(), "验证errorMessage");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SearchIdcardInfoById_004() {
		Logger.start(true, "不输入userId");
		
		try {
			
			searchIdcardInfoByIdReq.setIdcardId(1L);
			searchIdcardInfoByIdReq.setUserId(null);
			
			searchIdcardInfoByIdCall.setData(searchIdcardInfoByIdReq);
			searchIdcardInfoByIdCall.callService();
			
			Logger.verifyEquals(false, searchIdcardInfoByIdCall.getSuccess(), "验证success");
			Logger.verifyEquals(100, searchIdcardInfoByIdCall.getErrorCode(), "验证errorCode");
			Logger.verifyEquals("userId:must be greater than or equal to 1",
					searchIdcardInfoByIdCall.getMsg(), "验证errorMessage");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
